# This code is licensed under GPL v2.
# This code is developed by Amin Mahpour (2012)
import re
import urllib2 as U


"""adsfdsaDFS"""
class Updater():


    def __init__(self, url):
        """URL Initializes the update procedure"""
        ERR = False
        VERSION = 1.0
        FILE = U.urlopen(url)
        #FILE = open("update.txt","r")
        L = str(FILE.read()).splitlines()
        print """
UPDATER VERSION: %2.1f
There are %d file(s) to be updated
        """ % (VERSION,  len(L))
        FILE.close()
        for i in L:
            SEARCH = re.search("FILENAME=(\w+.\w+)\s+VERSION=(\d+.\d+)\s+DIRECTORY=(\w+)\s+URL=([\w/\\:\.]+)\s+CHANGELOG=([\w+\s\.]+)",i)
            ifile =  SEARCH.group(1)
            iver = SEARCH.group(2)
            idir = SEARCH.group(3)
            iURL = SEARCH.group(4)
            ilog = SEARCH.group(5)
            print "[Downloading] %s from %s" % (ifile,iURL)
            try:
                url = U.urlopen(iURL)
                OUT = open(ifile,"w")
                print "[Installing] %s" % (ifile)
                OUT.write(url.read())
                OUT.close()
                print "[ver:%s] %s successfully installed." % (iver, ifile)
            except U.URLError:
                print "[Error] Error in downloading from %s" % iURL
                ERR=True
        if ERR:
            print "Update finished with error"
        else:
            print "Successfully updated!"    






